package com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.service.impl;

import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.bean.LimsQualitycontrolTestBatchBean;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.bean.LimsQualitycontrolTestResultBean;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.dao.LimsQualitycontrolTestBatchDao;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.service.LimsQualitycontrolTestBatchService;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolresult.service.LimsQualitycontrolTestResultService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsQualitycontrolTestBatchServiceImpl implements LimsQualitycontrolTestBatchService {

    @Autowired
    private LimsQualitycontrolTestBatchDao limsQualitycontrolTestBatchDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsQualitycontrolTestBatchDao getDao() {
        return limsQualitycontrolTestBatchDao;
    }

    @Autowired
    private LimsQualitycontrolTestResultService limsQualitycontrolTestResultService;

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsQualitycontrolTestBatchBean limsQualitycontrolTestBatch = jsonWrapper.parseUnique(LimsQualitycontrolTestBatchBean.class);
        limsQualitycontrolTestBatch.setId(ApplicationContextHelper.getNextIdentity());
        limsQualitycontrolTestBatch.setQualityControlTestBatchCode(ApplicationContextHelper.getNextSequence(getDao().getTable()));
        getDao().insert(limsQualitycontrolTestBatch);
        return limsQualitycontrolTestBatch.getId();
    }

    @Override
    public String commitTo(RestJsonWrapperBean wrapper) {
        List<LimsQualitycontrolTestBatchBean> batchBeans = wrapper.parse(LimsQualitycontrolTestBatchBean.class);
        List<Long> batchIds = batchBeans.stream().map(LimsQualitycontrolTestBatchBean::getId).collect(Collectors.toList());


        List<LimsQualitycontrolTestResultBean> resultBeans =
                limsQualitycontrolTestResultService.selectListByFilter(SearchFilter.instance().match("controlTestBatchId", batchIds).filter(MatchPattern.OR));
        batchBeans.forEach(i -> {
            i.setStatus("1");
            resultBeans.stream().filter(limsQualitycontrolTestResultBean -> limsQualitycontrolTestResultBean.getControlTestBatchId().equals(i.getId())).forEach(limsQualitycontrolTestResultBean -> {
                limsQualitycontrolTestResultBean.setStatus("1");
                limsQualitycontrolTestResultBean.setQualityControlTestBatchCode(i.getQualityControlTestBatchCode());
            });
        });
        getDao().update(batchBeans, "status");
        limsQualitycontrolTestResultService.getDao().update(resultBeans, "status");
        return "";
    }
}
